﻿#ifndef XScale_H
#define XScale_H

#include <QLabel>
#include <QPainter>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QApplication>
#include <QDebug>

#include "xnamespace.h"

QT_BEGIN_NAMESPACE

class XScalePrivate;
class XScale : public QLabel
{
    Q_OBJECT

    Q_PROPERTY(int mainScaleNumber READ getMainScaleNumber WRITE setMainScaleNumber)
    Q_PROPERTY(int subScaleNumber READ getSubScaleNumber WRITE setSubScaleNumber)

    Q_PROPERTY(int maximumRange READ getMaximumRange WRITE setMaximumRange)
    Q_PROPERTY(int minimumRange READ getMinimumRange WRITE setMinimumRange)

    Q_PROPERTY(int precision READ getPrecision WRITE setPrecision)
    Q_PROPERTY(bool unitStringEnable READ getUnitStringEnable WRITE setUnitStringEnable)
    Q_PROPERTY(QString unitString READ getUnitString WRITE setUnitString)
    Q_PROPERTY(QColor unitStringColor READ getUnitStringColor WRITE setUnitStringColor)

    Q_PROPERTY(QColor mainScaleColor READ getMainScaleColor WRITE setMainScaleColor)
    Q_PROPERTY(QColor baseScaleColor READ getBaseScaleColor WRITE setBaseScaleColor)
    Q_PROPERTY(QColor scaleValueColor READ getScaleValueColor WRITE setScaleValueColor)

    Q_PROPERTY(ScaleChartDirection scaleDirection READ getScaleDirection WRITE setScaleDirection)
    Q_PROPERTY(bool scaleValueReverseOrder READ getScaleValueReverseOrder WRITE setScaleValueReverseOrder)

    Q_PROPERTY(QColor borderColor READ getBorderColor WRITE setBorderColor)
    Q_PROPERTY(QColor backgroundColor READ getBackgroundColor WRITE setBackgroundColor)

public:
    explicit XScale(QWidget *parent = nullptr, Qt::WindowFlags f=Qt::WindowFlags());
    ~XScale();

    /* Property Interface */
    int getMainScaleNumber() const;
    void setMainScaleNumber(int);

    int getSubScaleNumber() const;
    void setSubScaleNumber(int);

    int getMaximumRange() const;
    void setMaximumRange(int);

    int getMinimumRange() const;
    void setMinimumRange(int);

    int getPrecision() const;
    void setPrecision(int);

    bool getUnitStringEnable() const;
    void setUnitStringEnable(bool);

    QString getUnitString() const;
    void setUnitString(const QString string);

    QColor getMainScaleColor() const;
    void setMainScaleColor(const QColor &color);

    QColor getUnitStringColor() const;
    void setUnitStringColor(const QColor &color);

    QColor getBaseScaleColor() const;
    void setBaseScaleColor(const QColor &color);

    QColor getScaleValueColor() const;
    void setScaleValueColor(const QColor &color);

    ScaleChartDirection getScaleDirection() const;
    void setScaleDirection(const ScaleChartDirection type);

    bool getScaleValueReverseOrder() const;
    void setScaleValueReverseOrder(bool order);

    QColor getBorderColor() const;
    void setBorderColor(const QColor &color);

    QColor getBackgroundColor() const;
    void setBackgroundColor(const QColor &color);

protected:
    void paintEvent(QPaintEvent *ev) override;
    void drawBorder(QPainter *painter);
    void drawBg(QPainter *painter);
    virtual void drawRuler(QPainter *painter);

    void enterEvent(QEvent *ev) override;
    void leaveEvent(QEvent *ev) override;
    void mousePressEvent(QMouseEvent *ev) override;
    void mouseReleaseEvent(QMouseEvent *ev) override;
    void mouseMoveEvent(QMouseEvent *ev) override;

private:
    Q_DISABLE_COPY(XScale)
    XScalePrivate *xScaleP = nullptr;

    friend class XScaleChart;

    virtual void getUnitStringParameters(QPainter *painter);
    virtual void getScaleValueParameters(QPainter *painter);
    virtual void getBaseLineParameters();
    virtual void getScaleLineLength();
    virtual void getScaleLinePoint(QPainter *painter, QPointF &startPoint, QPointF &endPoint, const int index);
    virtual void getScaleValueTextPoint(QPainter *painter, QString &text, QPointF &textPoint, const QPointF &endPoint, const int index);
};

QT_END_NAMESPACE

#endif // XScale_H
